<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users_Groups extends CI_Controller {
	var $perpage = 20;

		public function __construct() {
		   parent::__construct();
		   if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) redirect('auth/login/'.str_replace("/", ".",$_SERVER['REQUEST_URI']));
		   $this->layout->setlayout('admin/layout');
		   $this->crumps->push('/admin/users_groups/', 'Группы пользователей');
		   $this->users_rights_model->check_rights(strtolower(get_class()), $this->router->get_method());
	   }

	public function index()	{

		$data['from'] = $from = $this->input->get('from', true);
		if ($from == "") $from = 0;
		$data['pages'] = $pages = $this->db->select()->from('groups')->limit($this->perpage, $from)->get()->result_array();

		/* pagination */
		$count = $this->db->select()->from('users')->count_all_results();
		if ($from != 0 && sizeof($pages) <= 0 && $count > 0) {
			$pgng = $from - $this->perpage;
			redirect('admin/users_groups/index/?from='.$pgng);
		}
		$this->load->library('pagination');
		$config['base_url'] = '/admin/users_groups/index/';
		$config['total_rows'] = $count;
		$config['page_query_string'] = TRUE;
		$config['per_page'] = $this->perpage;
		$this->pagination->initialize($config);
		$data['paging'] = $this->pagination->create_links();
		/* pagination */
		$this->layout->view('admin/users_groups/index', $data);
	}
	public function edit($id = 0) {
		$this->crumps->push('/admin/users_groups/'.($id == 0?'add':'edit/'.$id).'/', ($id == 0?"Добавление":"Редактирование"));
		if ($id != 0) $data['page'] = $page = $this->db->select()->from('groups')->where('id', $id)->get()->row_array();
		if ($id != 0 && sizeof($page) <= 0) redirect('admin/users_groups');

		$page['password'] = "";

		$form = new jQuickForm('simple');

		$form->setLabel(($id == 0?"Добавление":"Редактирование").' группы');

		$gr = $form->insertGroup()->addClass('col col2');

		$gr->insertInputText('name')->setLabel('Имя')->addRuleRequired();
		$gr->insertInputText('description')->setLabel('Название')->addRuleRequired();

		if ($_SERVER['REQUEST_METHOD'] == "POST" && $form->validate()) {
			if ($id == 0) {
				$p_data = $this->input->post();
				$this->db->insert('groups', $p_data);
				$result = $this->db->insert_id();
				$this->session->set_flashdata('msg', 'Успешно добавлено');
				redirect('admin/users_groups/edit/'.$result.'/');
			} else {
				$p_data = $this->input->post();
				$this->db->update('groups', $p_data, array('id' => $id));
				$this->session->set_flashdata('msg', 'Успешно обновлено');
				redirect('admin/users_groups/edit/'.$id.'/');
			}
		}
		$form->insertInputSubmit(($id == 0?"Добавить":"Сохранить"));

		$form->addDataSource(new HTML_QuickForm2_DataSource_Array($page));
		$data['form'] = $form."";
		$data['js'] = jQuickForm::js()->getJsAll();
		$this->layout->view('admin/users_groups/edit', $data);
	}
	public function delete($id = 0) {
		$from = $this->input->get('from', true);
		if ($from != "" && is_numeric($from)) $pgng = "?from=".$from;
		if ($id != 0) $data['page'] = $page = $this->db->select()->from('groups')->where('id', $id)->get()->result_array();
		if ($id != 0 && sizeof($page) <= 0) redirect('admin/users_groups');
		$this->db->delete('groups', array('id'=>$id));
		$this->db->update('users', array('group_id' => '2'), array('group_id' => $id));
		$this->session->set_flashdata('msg', 'Успешно удалено');
		redirect('admin/users_groups/'.$pgng);
	}
	public function add() {
		return $this->edit(0);
	}
}